package cn.ffcs.unm.majorSkill.action;

import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.unm.majorSkill.action.bean.BackboneTeamMainBean;
import cn.ffcs.unm.majorSkill.manager.MajorSkillManager;
import cn.ffcs.unm.majorSkill.model.BackboneTeam;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

@Controller
@Scope("prototype")
public class BackboneTeamMainComposer extends BasePortletComposer {

	private static final long serialVersionUID = 4379573077021746516L;

	private BackboneTeamMainBean bean = new BackboneTeamMainBean();
	/**
	 * 查询实体
	 */
	private BackboneTeam qryBackboneTeam;

	@SuppressWarnings("unused")
	private BackboneTeam backboneTeam;

	@Autowired
	@Qualifier("majorSkillManager")
	private MajorSkillManager majorSkillManager;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化
	 * 
	 * @throws Exception
	 */
	public void onCreate$backboneTeamMainWin() throws Exception {
		//this.onQuery();
	}

	/**
	 * 查询
	 */
	public void onQuery() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		if (!StrUtil.isNullOrEmpty(bean.getMonthId().getValue())) {
			String monthId = formatter.format(bean.getMonthId().getValue());
			clearVariable();
			BackboneTeam objBackboneTeam = BackboneTeam.newInstance();
			PubUtil.fillPoFromBean(bean, objBackboneTeam);
			qryBackboneTeam = objBackboneTeam;
			int index = bean.getRadiogroup().getSelectedIndex();
			if (index == 0) {
				String monthIdMonth = monthId.substring(0, 6);
				qryBackboneTeam.setMonthId(monthIdMonth);
			}else {
				qryBackboneTeam.setMonthId(monthId);
			}
			this.bean.getBackboneTeamListboxPaging().setActivePage(0);
			onBackboneTeam();
			backboneTeam = null;
		}else{
			ZkUtil.showError("请输入账期", "温馨提示");
		}
		
	}

	public void onBackboneTeam() {
		if (this.qryBackboneTeam != null) {
			int activePage = bean.getBackboneTeamListboxPaging()
					.getActivePage();
			int pageSize = bean.getBackboneTeamListboxPaging().getPageSize();
			PageInfo pageInfo = majorSkillManager.queryInfoByBackboneTeam(
					qryBackboneTeam, activePage + 1, pageSize);
			ListModelList dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getBackboneTeamListbox().setModel(dataList);
			bean.getBackboneTeamListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}

	/**
	 * 重置
	 */
	public void onReset() {
		bean.getName().setValue(null);
		bean.getCode().setValue(null);
		bean.getMonthId().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getBackboneTeamListbox());
		this.bean.getBackboneTeamListboxPaging().setTotalSize(1);
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryBackboneTeam = null;
		backboneTeam = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.onBackboneTeam();
	}

}
